// component/messageList/messageList.js
import {formateTime} from '../../utils/message'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    talksData:{
      type:Object,
      value:{},
      observer:function(newval){
        if (JSON.stringify(newval) !== '{}'){
        this.data.talksData = newval.messages[(newval.messages).length - 1]   //获取最后一条信息
        wx.cloud.database().collection('users').where({   //获取对话者的信息
          userId:newval.talkers[0]
        }).get().then(res=>{
          var speakerInfo = res.data[0]
          var info = wx.getStorageSync('userInfo')
          if (this.data.talksData.userId == info.userId) {
            this.setData({
              lastSpeakerName:info.userName,
              talksData:this.data.talksData,
              speakerInfo:speakerInfo,
              lastSpeakeTime:formateTime(this.data.talksData.publicTime)
            })
        }else if(this.data.talksData.userId == speakerInfo.userId){
          this.setData({
            lastSpeakerName:speakerInfo.userName,
            talksData:this.data.talksData,
            speakerInfo:speakerInfo,
            lastSpeakeTime:formateTime(this.data.talksData.publicTime)
          })
        }
        })
        }
       
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    talksData:'', //最后一条谈话信息
    lastSpeakerName:'',
    lastSpeakeTime:'',
    speakerInfo:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toTalking(){
      wx.navigateTo({
        url:`../../pages/talking/talking?id=${this.data.speakerInfo.userId}`,
      })
    }
  }
})
